<template>
  <div class="app-main-content">
    <el-form ref="queryForm" :inline="true" :model="queryForm" label-suffix=" :" @submit.native.prevent>
      <el-form-item label="查询条件" prop="value">
        <el-input v-model.trim="queryForm.value" placeholder="请输入角色名称" @keyup.enter.native="onSearch('queryForm')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch('queryForm')">查询</el-button>
        <el-button @click="onReset('queryForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      <el-button type="danger" icon="el-icon-delete" :loading="delBtnLoading" :disabled="!selected.length" @click="handleDeleteAll">删除</el-button>
    </el-row>
    <scmsa-table
      v-loading="tableLoading"
      class="margin-top-12"
      :data="tableData"
      :column="tableColumn"
      border
      :pagination="true"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="totalPage"
      @size-change="handlePageSizeChange"
      @pagination-current-change="handlePageCurrentChange"
      @selection-change="handleSelectionChange"
    />
    <el-drawer
      ref="drawer"
      :title="title"
      :visible.sync="dialog"
      direction="rtl"
      size="400px"
      :destroy-on-close="true"
      :modal-append-to-body="false"
      :wrapper-closable="false"
      @close="resetFieldsForm('actionForm')"
    >
      <div class="drawer__content">
        <el-form ref="actionForm" :model="actionForm" :rules="actionRules" label-suffix=" :" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model.trim="actionForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="describe">
            <el-input type="textarea" v-model.trim="actionForm.describe" />
          </el-form-item>
        </el-form>
        <div class="drawer__footer">
          <el-button @click="dialog = false">取 消</el-button>
          <el-button type="primary" :loading="loading" @click="onSubmit('actionForm')">{{ loading ? '提交中 ...' : '确定' }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  export default {
    name: 'SingleTableNormal',
    data() {
      return {
        action: 'add',
        tableLoading: false,
        currentPage: 1,
        pageSize: 10,
        loading: false,
        delBtnLoading: false,
        selected: [],
        dialog: false,
        editIndex: 0,
        actionRules: {
          name: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ]
        },
        actionForm: {
          name: '',
          describe: ''
        },
        queryForm: {
          value: ''
        },
        params: {
          page: 1,
          limit: 10
        },
        tableData: [
          {
            id: new Date().getTime(),
            name: 'UI设计师',
            describe: '指从事对软件的人机交互、操作逻辑、界面美观的整体设计工作的人'
          }
        ],
      }
    },
    computed: {
      totalPage() {
        return this.tableData.length
      },
      title() {
        return (this.action === 'add' ? '新增' : '编辑') + '角色'
      },
      tableColumn() {
        return [{
          type: 'selection',
          width: 55,
          align: 'center'
        }, {
          prop: 'name',
          label: '角色名称'
        }, {
          prop: 'describe',
          label: '角色描述'
        }, {
          label: '操作',
          width: 200,
          render: (h, params) => {
            return h('div', [
              h('el-button', {
                props: {
                  type: 'text',
                  size: 'small',
                  icon: 'el-icon-edit',
                  loading: false,
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleEdit(params)
                  }
                }
              }, '编辑'),
              h('el-divider', {
                props: {
                  direction: 'vertical'
                }
              }),
              h('el-button', {
                props: {
                  type: 'text',
                  size: 'small',
                  icon: 'el-icon-delete',
                  loading: false
                },
                on: {
                  click: () => {
                    this.handleDelete(params)
                  }
                }
              }, '删除')
            ])
          }
        }]
      }
    },
    methods: {
      // 每页条数发生改变时返回值
      handlePageSizeChange(v) {
        this.pageSize = v
      },
      // 当前选中页发生改变时返回值
      handlePageCurrentChange(v) {
        this.currentPage = v
      },
      // checkbox 当选择项发生变化时会触发该事件
      handleSelectionChange(v) {
        this.selected = v
      },
      // 重置form
      resetFieldsForm(formName) {
        this.$refs[formName].resetFields()
        this.actionForm = {
          name: '',
          describe: ''
        }
      },
      // 提交
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true
            const form = this.action === 'add' ? {...JSON.parse(JSON.stringify(this.actionForm)), id: new Date().getTime()} : JSON.parse(JSON.stringify(this.actionForm))
            setTimeout(() => {
              this.loading = false
              this.dialog = false
            }, 200)
            if (this.action === 'add') {
              this.tableData.push(form)
            } else {
              this.$set(this.tableData, this.editIndex, form)
            }
          } else {
            return false
          }
        })
      },
      // 查询重置
      onReset(formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        // 发起请求
      },
      // 查询搜索
      onSearch() {
        this.currentPage = 1
        // 发起请求
      },
      // 点击添加
      handleAdd() {
        this.action = 'add'
        this.dialog = true
      },
      // 批量删除
      handleDeleteAll() {
        this.$confirm('确定执行删除操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          const ids = this.selected.map(item => item.id)
          this.tableData = this.tableData.filter(item => !ids.includes(item.id))
          this.delBtnLoading = false
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 删除
      handleDelete(params) {
        this.$confirm('确定执行删除操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(params.$index, 1)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 编辑
      handleEdit(params) {
        this.action = 'edit'
        this.editIndex = params.$index
        this.actionForm = JSON.parse(JSON.stringify(params.row))
        this.dialog = true
      }
    }
  }
</script>

<style scoped>
</style>
